package cn.lanqiao.book.controller;


import cn.lanqiao.book.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/*
* 用于处理 重置密码请求
* username 用户名
* Vcode 验证码
* password 新密码
* */
@WebServlet("/reset")
public class ResetPassword extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session=req.getSession();
        //获取用户输入的验证码
        int num1= Integer.parseInt(req.getParameter("Vcode"));
        //获取系统生成的验证码
        int num2=(int)session.getAttribute("number");
        String username=req.getParameter("username");
        String NewPassword=req.getParameter("password");
        if(num1!=num2){
            //保存错误信息
            req.setAttribute("msg","验证码错误");
            //  再次返回修改密码阶段 reset.jsp为修改密码界面
            req.getRequestDispatcher("/forgetpwd.jsp").forward(req,resp);
        }else {
            //重置密码
            UserServiceImpl.getInstance().resetPassword(username,NewPassword);
           resp.sendRedirect("/index.jsp");
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
